﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Data;

namespace TCC.DB
{
    public class DbBase
    {
        string conStr = @"Data Source=.\SQLEXPRESS;AttachDbFilename=E:\TCC\ownztec\ProjetoTCC\TCC\App_Data\TCCDB.mdf;Integrated Security=True;User Instance=True";
        private SqlConnection sqlCon;
        private SqlDataAdapter sqlDa;
        private SqlCommand sqlCmd;
        private SqlDataReader sqlDr;

        public DbBase()
        {
            sqlCon = new SqlConnection(conStr);
        }

        private void Open()
        {
            sqlCon.Open();
        }

        private void Close()
        {
            try
            {
                if (sqlCon.State != System.Data.ConnectionState.Closed)
                    sqlCon.Close();
            }
            catch
            { }
        }

        protected void ExecuteNonQuery(string Query)
        {
            sqlCmd = new SqlCommand(Query, sqlCon);

            Open();

            sqlCmd.ExecuteNonQuery();

            Close();
        }

        protected DataTable Fill(string Query)
        {
            var res = new DataTable();

            sqlCmd = new SqlCommand(Query, sqlCon);
            sqlDa = new SqlDataAdapter(sqlCmd);

            sqlDa.Fill(res);

            return res;
        }

        protected object ExecuteScalar(string Query)
        {
            sqlCmd = new SqlCommand(Query, sqlCon);

            Open();

            var res = sqlCmd.ExecuteScalar();

            Close();

            return res;
        }
    }
}